import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'font-awesome/css/font-awesome.min.css'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'
import httpRequest from './utils/request'
import { initMenu } from './utils/menus'


Vue.config.productionTip = false

//引入element-ui
Vue.use(ElementUI);
//设置axios请求的全局变量
Vue.prototype.$http = httpRequest;


//全局前置守卫
router.beforeEach((to, from, next) => {
  //有token就去初始化菜单
  if(window.sessionStorage.getItem("tokenStr")){
    initMenu(router,store);
    //判断当前用户是否存在
    if(!store.state.currentAdmin){
        //将当前用户存到vuex中
        store.dispatch("getCurrentAdmin");
    }
    next();
  }else{
    //如果用户未登录访问内部页面则跳至登录页面并给定跳转值
    if(to.path == '/'){
        next();
    }else{
        ElementUI.Message.error({message:'请先登录！'});
        next(`?redirect=${to.path}`);
    }
  }
  
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
